﻿using System;
using com.yoozoo.gta.Extension.UGUI.ScreenOrientationEx;
using UnityEngine;
using Yoozoo.UI.YGUI;

namespace com.yoozoo.gta.Extension.UGUI.ScreenOrientation.Adapter
{
    
    
    [Serializable]
    public class YScrollRectConfig {
        public YScrollRect.Direction direction;
        public int constraintCount;
        public Vector2 GridSpacing = YGridViewAdapter.DefaultGridSpacing;
        public bool UseYTableViewCellSize = true;  //是否通过YTableViewCell的size 来设置LayoutGroup的cellSize
        
        public override bool Equals(object obj) {
            if (obj == null) {
                return false;
            }

            YScrollRectConfig other = (YScrollRectConfig)obj;
            //对比下面属性是否一致
            if (direction != other.direction) {
                return false;
            }

            // Compare constraintCount properties
            if (constraintCount != other.constraintCount) {
                return false;
            }
            
            // Compare GridSpacing properties
            if (GridSpacing != other.GridSpacing) {
                return false;
            }
            
            // Compare UseYTableViewCellSize properties
            if (UseYTableViewCellSize != other.UseYTableViewCellSize) {
                return false;
            }
            
            return true;
        }
        
        public override int GetHashCode() {
            return base.GetHashCode();
        }
    }
    
    [DisallowMultipleComponent]
    public class YGridViewAdapter : AdaptBase<YScrollRectConfig, YGridView>
    {
        public static Vector2 DefaultGridSpacing = new Vector2(-1,-1);
        public override int Order => (int)AdapterExecuteOrder.ListView; 
        protected override void CopyProperty(YScrollRectConfig config) {
            base.CopyProperty(config);
            config.direction = mComponent.direction;
            config.constraintCount = mComponent.contentConstraintCount;
            config.GridSpacing = mComponent.GridSpacing;
            config.UseYTableViewCellSize = mComponent.UseYTableViewCellSize;
        }

        protected override void ApplyConfig(YScrollRectConfig config) {
            base.ApplyConfig(config);
            mComponent.direction = config.direction;
            mComponent.ChangeColumnSize(config.constraintCount);
            if (config.GridSpacing != DefaultGridSpacing)
            {
                mComponent.GridSpacing = config.GridSpacing;
            }
            mComponent.UseYTableViewCellSize = config.UseYTableViewCellSize;
            if (mComponent.UseYTableViewCellSize)
            {
                var yTableViewCell = mComponent.getCellPrefab();
                var layoutElementAdapter = yTableViewCell.GetComponent<LayoutElementAdapter>();
                if (layoutElementAdapter)
                {
                    if (currentConfigIndex == 2) //说明要切成横的
                    {
                        layoutElementAdapter.ApplyLandscapeConfig();
                    }else if(currentConfigIndex == 1)
                    {
                        layoutElementAdapter.ApplyPortraitConfig();
                    }
                }
               
            }
        }
    }

}